﻿/*
 * Copyright 2008 Pimm Hogeling, Karim Sempel and Demian Sempel
 * 
 * This file is part of 8bitplays.
 * 
 * 8bitplays is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * 8bitplays is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with 8bitplays. If not, see <http://www.gnu.org/licenses/>.
 */
package nl.sandmen.team.tools {
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	/**
	 * @author Team Sandmen
	 */
	public class GradientTool {
		private static var _backgroundGradient:BitmapData;
		private static var _fieldGradient:BitmapData;
		private static function get backgroundGradient():BitmapData {
			if (_backgroundGradient) {
				return _backgroundGradient;
			}
			var _backgroundGradient:BitmapData = new BitmapData(3, 3, false, 0xFFE8E8E8);
			for (var i:uint = 0; i < 4; i++) {
				_backgroundGradient.setPixel(i, i, 0xFFAAAAAA);
			}
			return _backgroundGradient;
		}
		private static function get fieldGradient():BitmapData {
			var _fieldGradient:BitmapData = new BitmapData(4, 4, true, 0x00000000);
			for (var i:uint = 0; i < 5; i++) {
				_fieldGradient.setPixel32(3 - i, i, 0xFFD8D8D8);
			}
			return _fieldGradient;
		}
		public static function createGradientBackgroundSprite(x:Number, y:Number, width:Number, height:Number):Sprite {
			var output:Sprite = new Sprite();
			output.x = x;
			output.y = y;
			output.graphics.beginBitmapFill(backgroundGradient);
			output.graphics.drawRect(0, 0, width, height);
			output.graphics.endFill();
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(width, height, Math.PI * .5);
			output.graphics.beginGradientFill(GradientType.LINEAR, new Array(0xAAAAAA, 0xAAAAAA), new Array(.8, 0), new Array(0, 0xFF), matrix);
			output.graphics.drawRect(0, 0, width, height);
			output.graphics.endFill();
			return output;
		}
		public static function createGradientFieldSprite(x:Number, y:Number, width:Number, height:Number):Sprite {
			var output:Sprite = new Sprite();
			output.x = x;
			output.y = y;
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(width, height, Math.PI * .5);
			var line:Sprite = output.addChild(new Sprite()) as Sprite;
			line.name = "line";
			line.graphics.lineStyle(1, 0xAAAAAA, .5);
			line.graphics.lineTo(width, 0);
			line.graphics.lineTo(width, height);
			line.graphics.lineTo(0, height);
			line.graphics.lineTo(0, 0);
			line.visible = false;
			output.graphics.beginGradientFill(GradientType.LINEAR, new Array(0xEEEEEE, 0xD8D8D8), new Array(1, 1), new Array(0, 0xFF), matrix);
			output.graphics.drawRect(0, 0, width, height);
			output.graphics.endFill();
			output.graphics.beginBitmapFill(fieldGradient);
			output.graphics.drawRect(0, 0, width, height);
			output.graphics.endFill();
			output.addEventListener(MouseEvent.MOUSE_OVER, showLine);
			output.addEventListener(MouseEvent.MOUSE_OUT, hideLine);
			return output;
		}
		private static function hideLine(e:MouseEvent):void {
			(e.currentTarget as Sprite).getChildByName("line").visible = false;
		}
		private static function showLine(e:MouseEvent):void {
			(e.currentTarget as Sprite).getChildByName("line").visible = true;
		}
	}
}